package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC885 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
            int[][] res = new int[R * C][2];
            int[][] around = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
            int x = r0, y = c0, num = 1, dir = 0;  //{x, y}为当前位置，num为当前查找的数字，dir为当前的方向
            int Left = c0 - 1, Right = c0 + 1, Upper = r0 - 1, Bottom = r0 + 1;  //四个方向的边界
            while (num <= R * C) {
                if (x >= 0 && x < R && y >= 0 && y < C) {  //{x， y}位置在矩阵中
                    res[num - 1] = new int[]{x, y};
                    num++;
                }
                if (dir == 0 && y == Right) {  //向右到右边界
                    dir += 1;  //调转方向向下
                    Right += 1;  //右边界右移
                } else if (dir == 1 && x == Bottom) {  //向下到底边界
                    dir += 1;
                    Bottom += 1;  //底边界下移
                } else if (dir == 2 && y == Left) {  //向左到左边界
                    dir += 1;
                    Left--;  //左边界左移
                } else if (dir == 3 && x == Upper) {  //向上到上边界
                    dir = 0;
                    Upper--;  //上边界上移
                }
                x += around[dir][0];   //下一个节点
                y += around[dir][1];
            }
            return res;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC885 an = new LC885();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
